const Payment = require('../models/Payment')
const Transaction = require('../models/Transaction')
const { Op } = require('sequelize')

class TransactionService {
  // 获取用户充值记录
  async getRechargeRecords(userId) {
    try {
      const records = await Payment.findAll({
        where: {
          userId,
          status: 'paid'
        },
        order: [['createdAt', 'DESC']],
        limit: 10
      })
      
      return records
    } catch (error) {
      console.error('获取充值记录失败:', error)
      throw error
    }
  }

  // 获取用户消费记录
  async getConsumptionRecords(userId) {
    try {
      const records = await Transaction.findAll({
        where: {
          userId,
          type: {
            [Op.in]: ['purchase_course', 'purchase_vip']
          }
        },
        order: [['createdAt', 'DESC']],
        limit: 10
      })
      
      return records
    } catch (error) {
      console.error('获取消费记录失败:', error)
      throw error
    }
  }
}

module.exports = new TransactionService()